package com.gcxx.framework.util.properties;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gcxx.framework.util.classloader.ClassLoaderUtil;
import com.gcxx.framework.util.string.StringUtil;

/**
 * <strong>Title : InitPropertiesUtil<br></strong>
 * <strong>Description : </strong>系统初始化配置工具类<br> 
 * <strong>Create on : 2012-10-24<br></strong>
 * <p>
 * <strong>Copyright (C) <br></strong>
 * <p>
 * @author 郭毅<br>
 * @version <strong>GCXX_FrameWork3.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人		修改日期		修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class InitPropertiesUtil {

	private static final Log logger = LogFactory.getLog(InitPropertiesUtil.class);

	/**
	 * 初始化配置文件名称
	 */
	private static final String CONFIGURATION_FILE = "init.properties";

	private InitPropertiesUtil(){}

	private static Properties properties;
	
	/**
	 * <code>postfixNJameMap</code>-名字后缀map集合
	 */
	private static Map<String, Set> postfixNJameMap;

	static{
		if(null == properties){
			properties = ClassLoaderUtil.getProperties(CONFIGURATION_FILE);
			if(logger.isInfoEnabled()){
				logger.info("INFO:System initialize successful!");
			}
		}
	}

	/**
	 * 根据key值返回初始化配置文件的value
	 * @param key值
	 * @return
	 */
	public static String getProperty(String key){
		return properties.getProperty(key);
	}
	
	/**
	 * 根据后缀名查询属性值集合
	 * @return
	 */
	public static Set<String> getPropertysBypPostfixName(String postfixName){
		if(StringUtil.isNotNullAndSNull(postfixName)){
			if(postfixNJameMap == null){
				postfixNJameMap = new HashMap<String, Set>();
				Set keySet = properties.keySet();
				Iterator it = keySet.iterator();
				while (it.hasNext()) {
					String key = (String) it.next();
					if(key.endsWith(postfixName)){
						Set valueSet = postfixNJameMap.get(postfixName);
						if(valueSet == null){
							valueSet = new HashSet<String>();
						}
						valueSet.add(properties.getProperty(key));
						postfixNJameMap.put(postfixName, valueSet);
					}
				}
			}
			
			return (Set)postfixNJameMap.get(postfixName);
			
		}
		return null;
	}

	/**
	 * 系统连接超时时是否弹出提示
	 * @return
	 */
	public static boolean isShowTimeOutMessage(){
		return properties.getProperty("is.show.timeout").equals("true");
	}

	/**
	 * 获取当前系统名称,默认为"GCXX FR 权限框架",该属性在init.properties中配置,其key为system.name
	 * @return
	 */
	public static String getSystemName(){
		return properties.getProperty("system.name", "GCXX FR 权限框架");
	}

	public static void main(String[] args) {
	}
}
